package carassius.DAL;

import carassius.BLL.Events.OrganismList.OrganismListAmountChangedListener;
import carassius.BLL.Events.OrganismList.OrganismListAmountChangedPublisher;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;

/**
 *
 * @author siebz0r
 */
public class OrganismListRow
		extends Row<SimpleEntry<Integer, Integer>>
		implements OrganismListAmountChangedPublisher
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final AquariumRow _aquarium;
	private final int _organismId;
	private int _amount;
	private final Object AMOUNT_LOCK;
	private final ArrayList<OrganismListAmountChangedListener> _organismListAmountChangedListeners;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public OrganismListRow(ResultSet resultSet) throws SQLException
	{
		super(new SimpleEntry<Integer, Integer>(resultSet.getInt("aquarium_id"), resultSet.getInt("organism_id")));
		_aquarium = Database.getAquariumTable().getByPrimaryKey(resultSet.getInt("aquarium_id"));
		_organismId = resultSet.getInt("organism_id");
		_amount = resultSet.getInt("amount");
		_organismListAmountChangedListeners = new ArrayList<OrganismListAmountChangedListener>();
		AMOUNT_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getAmount()
	{
		synchronized (AMOUNT_LOCK)
		{
			return _amount;
		}
	}

	public boolean setAmount(int amount)
	{
		boolean result = false;
		synchronized (AMOUNT_LOCK)
		{
			try
			{
				if (_amount != amount)
				{
					String sql = "UPDATE organism_list SET amount = ? where aquarium_id = ? and organism_id = ?";
					PreparedStatement st = Database.prepareStatement(sql);
					st.setInt(1, amount);
					st.setInt(2, _aquarium.getId());
					st.setInt(3, _organismId);

					if (st.executeUpdate() == 1)
					{
						_amount = amount;
						fireOrganismListAmountChangedEvent();
						result = true;
					}
				}
				else
				{
					result = true;
				}
			}
			catch (SQLException ex)
			{
				System.out.println(ex.toString());
			}
		}
		return result;
	}

	public AquariumRow getAquarium()
	{
		return _aquarium;
	}

	public int getOrganismId()
	{
		return _organismId;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final OrganismListRow other = (OrganismListRow) obj;
		if (this._aquarium != other._aquarium && (this._aquarium == null || !this._aquarium.equals(other._aquarium)))
		{
			return false;
		}
		if (this._organismId != other._organismId)
		{
			return false;
		}
		if (this._amount != other._amount)
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 5;
		return hash;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Events">
	@Override
	public void addOrganismListAmountChangedListener(
			OrganismListAmountChangedListener listener)
	{
		synchronized(_organismListAmountChangedListeners)
		{
			_organismListAmountChangedListeners.add(listener);
		}
	}

	@Override
	public void removeOrganismListAmountChangedListener(
			OrganismListAmountChangedListener listener)
	{
		synchronized(_organismListAmountChangedListeners)
		{
			_organismListAmountChangedListeners.remove(listener);
		}
	}

	private void fireOrganismListAmountChangedEvent()
	{
		synchronized(_organismListAmountChangedListeners)
		{
			for (OrganismListAmountChangedListener listener : _organismListAmountChangedListeners)
			{
				listener.organismListAmountChanged(this);
			}
		}
	}
	//</editor-fold>
}
